/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------

var viewport = require("mlib/dom/viewport"); // viewport
var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析


var virtualLink = require("mlib/util/virtualLink");
var ajax = require("mlib/io/ajax");
var lazyload = require("mlib/dom/lazyload");
var closest = require("mlib/dom/closest");
var touch = require("mlib/evt/touch");

var round = require("../plugin/util/round");
var error = require("../public/js/error");
var Alert = require("../layer/weuialert");
var weuiDialog = require("../layer/weuidialog");

var nodataRender = require("./blank-page.ejs"); // 页面总模板
var listRender = require("./list.ejs"); // 页面总模板



     

module.exports = function(node, opts) {
    //-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;

	var weui_dialog = null;	
	
	
    //-------------事件响应声明---------------
    var evtFuncs = {
		bookNotice:function(evt){
				var target = closest(evt.target,"span.order-notice",true),id=target.dataset.id;
				if(that.isLock()) {
					return;
				}
				that.lock();
				ajax({ 
					url: "/index.php?r=goods/goodsdetail&id="+ id+"&ocid="+ data.ocid,
					onSuccess: function(result) {
						that.unLock();
						weui_dialog = null;
						weui_dialog = weuiDialog({	
						//	shadeClose:false,
							lockScroll:false,						
							title:"预定须知",		
							content:result.bookingnotice		
						});		
						weui_dialog.bind("aftershow",function(layer){
							var dnodes = layer.data.nodeList;
							dnodes.content.parentElement.style.height = document.documentElement.clientHeight/2 + "px";
					   });
					   weui_dialog.init();
					},
					onError: function(xmlHttp) {
						that.unLock();
					}
				});
		}
	}

    //-------------子模块实例化---------------
    var initMod = function() {		
    	//console.log(date);
         virtualLink(function(evt){
         	location.href = evt.target.getAttribute("data-href");
         });
		custFuncs.getShowcase();
    }

    //-------------绑定事件------------------
    var bindEvents = function() {

			touch.on(nodeList.ticketlist, 'tap', "span.order-notice", evtFuncs.bookNotice);
	}

    //-------------自定义函数----------------
    var custFuncs = {
    	 emptyRender:function(){
       		document.body.firstChild.remove();
       		document.body.insertAdjacentHTML('AfterBegin', nodataRender());
    	 },
         getShowcase:function(){
    				ajax({ 
    					url: data.requestAPIUrl,
    					onSuccess: function(result) {
                           if(result.status>0){
                           	    if(result.data.goodsList.length>0){
                       	    		nodeList.ticketlist.insertAdjacentHTML('AfterBegin', listRender(result.data));  
                           	    }else{
    	                       		  error({"type":"empty","message":"优惠券适用商品被搬空了<br />去看看其他商品吧~"}); 
                           	    }
                               	                       	
                           }else{
                           		//error('error');  
                                error({"type":"empty","message":"优惠券适用商品被搬空了<br />去看看其他商品吧~"}); 
                           }
    					},
    					onError: function(xmlHttp) {
    						that.unLock();
    					}
    				});
         }
    };

    //-------------一切从这开始--------------
    var init = function(_data) {

        data = _data;

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);

        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();

    }

    //---------------暴露API----------------
    that.init = init;

    return that;
};